package l.l.w.leetcode._082_RemoveDuplicatesFromSortedListII;

/**
 * Created by llw on 2018/5/5/0005.
 */
public class 消除链表中的重复元素 {

    public ListNode solution(ListNode head){
        ListNode result = new ListNode(-1);
        result.next = head;
        ListNode cur = head, pre = result;
        result.next = head;
        while(cur != null){
            ListNode next = cur.next;
            if( next != null && cur.val == next.val){
                cur = next;
            }else{
                if(pre.next == cur){
                    pre.next = cur;
                    pre = cur;
                    cur = next;
                }else{
                    pre.next = next;
                    cur = next;
                }
            }
        }
        return result.next;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode node21 = new ListNode(2);
        ListNode node31 = new ListNode(3);
        ListNode node32 = new ListNode(3);
        ListNode node41 = new ListNode(4);
        ListNode node42 = new ListNode(4);
        ListNode node51 = new ListNode(5);
        head.next = node21;
        node21.next = node31;
        node31.next = node32;
        node32.next = node41;
        node41.next = node42;
        node42.next = node51;

        ListNode result = new 消除链表中的重复元素().solution(head);
        System.out.println(result);
    }
}
class ListNode {
     int val;
     ListNode next;
     ListNode(int x) { val = x; }
  }
